package com.meitianhui.convenitenbuy.module.wallet.presenter;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.base.BaseListResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.main.model.bean.UserToken;
import com.meitianhui.convenitenbuy.module.wallet.contract.WalletCardRecordContract;
import com.meitianhui.convenitenbuy.module.wallet.http.WalletApiDataSource;
import com.meitianhui.convenitenbuy.module.wallet.model.CardRecordBean;
import com.meitianhui.convenitenbuy.module.wallet.ui.activity.WalletCardRecordActivity;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2017/11/2.
 */

public class WalletCardRecordPresenter implements WalletCardRecordContract.Presenter {

    private WalletCardRecordActivity context;
    private final WalletCardRecordContract.View view;
    private Gson gson;

    public WalletCardRecordPresenter(WalletCardRecordActivity view,
                                     WalletCardRecordActivity context) {
        this.view = view;
        this.context = context;
        gson = new Gson();
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

    @Override
    public void getTradeRecode(int pageNum, String cardNo) {
        UserToken token = InfoProvider.getUserToken(App.getInstance());
        String userToken = token.getUser_token();
        String service = "finance.transPrepayCardPageFind";

        Map<String, String> params = new HashMap<>();
        params.put("member_id", token.getMember_id());
        params.put("card_no", cardNo);
        String jsonParams = gson.toJson(params);

        Map<String, String> pageString = new HashMap<>();
        pageString.put("page_no", pageNum + "");
        pageString.put("page_size", "20");
        String page = gson.toJson(pageString);

        WalletApiDataSource.getInstance()
                .getCardRecord(userToken, service, page, jsonParams)
                .subscribe(new ResponseObserver<BaseListResponse<CardRecordBean>>(context, true) {
                    @Override
                    public void onSuccess(BaseListResponse<CardRecordBean> response) {
                        view.getTradeRecodeBack(true, response.data.getList(), null);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        view.getTradeRecodeBack(false, null, msg);
                    }
                });
    }

}
